package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.INavDao;
import com.cn.wanxi.dao.impl.NavDaoImpl;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.service.INavService;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NavServiceImpl implements INavService {
    INavDao navDao = new NavDaoImpl();
    @Override
    public int addNav(NavModel navModel) {
        return navDao.addNav(navModel);
    }

    @Override
    public int deleteNav(int id) {
        return navDao.deleteNav(id);
    }

    @Override
    public int editNav(NavModel navModel) {
        return navDao.editNav(navModel);
    }

    @Override
    public List<NavModel> getNavList() {
        Jedis jedis = new Jedis();
        List<NavModel> list = null;
        try {
            if (jedis.exists("nav_id")){
                list = navDao.getNavListByRedis();
            }else {
                list = navDao.getNavList();
                navDao.setNavListInRedis(list);
            }
        }catch (Exception e){
            list = navDao.getNavList();
        }
        return list;
    }

    @Override
    public NavModel getNav(int id) {
        return navDao.getNav(id);
    }

    @Override
    public PageModel<NavModel> getNavs(Map<String ,Object> map) {
        PageModel pager = new PageModel();
        pager.setList(navDao.getNavs(map));
        pager.setTotal(navDao.getNavCount(map));
        return pager;
    }

    @Override
    public int updateShow(int id,int show) {
        NavModel navModel = new NavModel();
        navModel.setId(id);
        navModel.setIsShow(show);
        return navDao.updateShow(navModel);
    }
}
